package com.lanou.dllo.vc.adapter;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMGroupManager;
import com.easemob.exceptions.EaseMobException;
import com.lanou.dllo.vc.R;
import com.lanou.dllo.vc.db.InviteMessgeDao;
import com.lanou.dllo.vc.entity.InviteMessage;

import org.w3c.dom.Text;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by dllo on 15/11/23.
 */
public class AddFriendMessageAdapter extends BaseAdapter {
    private Context context;
    private InviteMessgeDao messgeDao;
    private List<InviteMessage> inviteMessages;
    private String name;

    private boolean isNotify = false;


    public AddFriendMessageAdapter(Context context, int textviewResourceId, List<InviteMessage> inviteMessages) {
        this.context = context;
        messgeDao = new InviteMessgeDao(context);
        this.inviteMessages = inviteMessages;
    }

    @Override
    public int getCount() {
        return inviteMessages != null && inviteMessages.size() > 0 ? inviteMessages.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return inviteMessages.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {

            convertView = View.inflate(context, R.layout.item_addfriendmessage, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        InviteMessage inviteMessage = inviteMessages.get(position);
        Log.i("lanou", inviteMessage.getFrom() + "");
        name = inviteMessage.getFrom();
        holder.name.setText(inviteMessage.getFrom());
        holder.status.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.status.setVisibility(View.GONE);
                holder.refuse.setVisibility(View.GONE);
                holder.statusTv.setText("同意请求");
                holder.statusTv.setVisibility(View.VISIBLE);
                inviteMessages = null;
                new AddTask().start();
            }
        });
        holder.refuse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.status.setVisibility(View.GONE);
                holder.refuse.setVisibility(View.GONE);
                holder.statusTv.setText("拒绝请求");
                holder.statusTv.setVisibility(View.VISIBLE);
                inviteMessages = null;
                new Refuse().start();
            }
        });

        return convertView;
    }

    class AddTask extends Thread {//同意好友申请
        @Override
        public void run() {
            super.run();
            try {
                EMChatManager.getInstance().acceptInvitation(name);

                isNotify = true;

                ContentValues values = new ContentValues();
                values.put(InviteMessgeDao.COLUMN_NAME_STATUS,name);
                messgeDao.insert(values);

                Log.i("lanou", name + "--------------->同意了请求");


            } catch (EaseMobException e) {
                e.printStackTrace();
            }

        }
    }

    class  Refuse extends  Thread{//拒绝好友请求
        @Override
        public void run() {
            super.run();
            try {
                EMChatManager.getInstance().refuseInvitation(name);
                Log.i("lanou","拒绝好友成功");

            } catch (EaseMobException e) {
                e.printStackTrace();
            }
        }
    }

    static class ViewHolder {
        @Bind(R.id.avatar)
        ImageView avator;
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.message)
        TextView reason;
        @Bind(R.id.ll_group)
        LinearLayout groupContainer;
        @Bind(R.id.tv_groupName)
        TextView groupname;
        @Bind(R.id.add_user_state)
        Button status;
        @Bind(R.id.add_user_refuse)
        Button refuse;
        @Bind(R.id.statusTv)
        TextView statusTv;
        ViewHolder(View view) {
            ButterKnife.bind(this, view);
            status.setVisibility(View.VISIBLE);
            refuse.setVisibility(View.VISIBLE);
            statusTv.setVisibility(View.GONE);
        }
    }
}
